/*
包含n 个接口请求函数的模块
每个函数返回promise
*/
import ajax from './ajax'
import jsonp from 'jsonp'
// 登陆
export const reqLogin = (username, password) => ajax('/login', { username, password }, 'POST')
/**获取天气
 * @param {*} citykey：城市代码
 * @return {*} 
 */
export const getweather = (citykey) => ajax(`http://wthrcdn.etouch.cn/weather_mini?citykey=${citykey}`)
/**获取一级或某个二级分类列表
 * @param {*} parentId：父级分类的ID
 * @return {*} 
 */
export const getcategory = (parentId) => ajax(`/manage/category/list?parentId=${parentId}`)
export const addcategory = (parentId, categoryName) => ajax(`/manage/category/add`, { parentId, categoryName }, 'POST')
export const updatecategory = (parentId, categoryName) => ajax(`/manage/category/update`, { categoryId: parentId, categoryName }, 'POST')
export const getcity = () => {
  return new Promise((res, rej) => {
    const url = `http://api.map.baidu.com/location/ip?ak=V3B3oZD4bePKFIppcOfHgEH8ufMlw7DX&coor=bd09ll`
    jsonp(url, {}, (err, data) => {
      if (!err && data.status === 0) { res(data) } else { rej(err) }
    })
  })
}

export const getproductlist = (pageNum, pageSize) => ajax('/manage/product/list', { pageNum, pageSize })
export const updateStatus = (productId, status) => ajax('/manage/product/updateStatus', { productId, status }, 'POST')
export const searchproduct = ({ pageNum, pageSize, productName = null, productDesc = null }) => {
  if (productName === null) {
    return ajax(`/manage/product/search`, { pageNum, pageSize, productDesc })
  } else {
    return ajax(`/manage/product/search`, { pageNum, pageSize, productName })
  }
}
export const addproduct = (categoryId, pCategoryId, name, price, detail, desc, imgs) => ajax('/manage/product/add', { categoryId, pCategoryId, name, price, detail, desc, imgs }, 'POST')
export const editproduct = (_id, categoryId, pCategoryId, name, price, detail, desc, imgs) => ajax('/manage/product/update', { _id, categoryId, pCategoryId, name, price, detail, desc, imgs }, 'POST')
export const getinfo = (categoryId) => ajax(`/manage/category/info`, { categoryId })
export const deleteimg = (name) => ajax('/manage/img/delete', { name }, 'POST')
export const getrole = () => ajax('/manage/role/list')
export const addrole = (roleName) => ajax('/manage/role/add', { roleName }, 'POST')
export const updaterole = (role) => ajax('/manage/role/update', role, 'POST')
export const getuser = () => ajax('/manage/user/list')
export const adduser = (username, password, phone, email, role_id) => ajax('/manage/user/add', { username, password, phone, email, role_id }, 'POST')
export const edituser = (user) => ajax('/manage/user/update', user, 'POST')
export const deluser=(userId)=>ajax('/manage/user/delete',{userId},'POST')